package org.example.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class Login {
    @PostMapping("/login")
    public Object login(String username, String password, HttpServletRequest
            req){
        Map<String, Object> map = new HashMap<>();
//模拟用户登录时，用户名和密码校验
        if("abc".equals(username) && "123".equals(password)){
//登录成功，创建session
            HttpSession session = req.getSession();
//添加用户身份信息到session
            session.setAttribute("user", username+", "+password);
            map.put("用户名", username);
            map.put("密码", password);
        }
        return map;
    }
}
